

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="深海火锅店">
  <meta name="keywords" content="">
  
    <meta name="description" content="拼接字符串串接及其他一些函数在不同类型数据库实现里略有不同，假设要把JOHN和SON串接起来形成JOHNSON  Oracle的代码:SELECT ‘JOHN’ || ‘SON’,… sQLServer的代码:SELECT ‘JOHN’+’SON’,… MySQL的代码: SELECT CONCAT(‘JOHN’, ‘SON’)…MySQL中的串接函数可以连接多个字符串  TRANSLATE字符函">
<meta property="og:type" content="article">
<meta property="og:title" content="DB12_数据库函数1">
<meta property="og:url" content="http://example.com/2022/12/26/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB12&DB13/DB12_%E6%95%B0%E6%8D%AE%E5%BA%93%E5%87%BD%E6%95%B01/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="拼接字符串串接及其他一些函数在不同类型数据库实现里略有不同，假设要把JOHN和SON串接起来形成JOHNSON  Oracle的代码:SELECT ‘JOHN’ || ‘SON’,… sQLServer的代码:SELECT ‘JOHN’+’SON’,… MySQL的代码: SELECT CONCAT(‘JOHN’, ‘SON’)…MySQL中的串接函数可以连接多个字符串  TRANSLATE字符函">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-12-26T04:40:00.000Z">
<meta property="article:modified_time" content="2022-12-28T01:04:44.000Z">
<meta property="article:author" content="深海火锅店">
<meta property="article:tag" content="大数据">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>DB12_数据库函数1 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/background.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="DB12_数据库函数1"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-12-26 12:40" pubdate>
          2022年12月26日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.5k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          38 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">DB12_数据库函数1</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="拼接字符串"><a href="#拼接字符串" class="headerlink" title="拼接字符串"></a>拼接字符串</h3><p>串接及其他一些函数在不同类型数据库实现里略有不同，假设要把JOHN和SON串接起来形成JOHNSON</p>
<ul>
<li>Oracle的代码:SELECT ‘JOHN’ || ‘SON’,…</li>
<li>sQLServer的代码:SELECT ‘JOHN’+’SON’,…</li>
<li>MySQL的代码: SELECT CONCAT(‘JOHN’, ‘SON’)…<br>MySQL中的串接函数可以连接多个字符串</li>
</ul>
<h3 id="TRANSLATE字符函数"><a href="#TRANSLATE字符函数" class="headerlink" title="TRANSLATE字符函数"></a>TRANSLATE字符函数</h3><p>TRANSLATE函数搜索字符串里的字符并查找特定的字符，标记找到的位置，然后用替代字符串里对应的字符替换它。</p>
<p>– oracle 把字符串里的空格、星号、斜线、单引号都替换为下划线<br>select translate (‘SQL*Plus User’’s Guide’,’ *&#x2F;‘’’,’_’) from dual;</p>
<p>mysql目前没有translate，可以用多次replace进行</p>
<h3 id="REPLACE字符函数"><a href="#REPLACE字符函数" class="headerlink" title="REPLACE字符函数"></a>REPLACE字符函数</h3><p>REPLACE函数用于把某个字符或字符串替换为指定的一个字符(或多个字符)，其使用类似于TRANSLATE函数，只是它是把一个字符或字符串替换到另一个字符串里，其语法是:<br>REPLACE(‘value’ , ‘value’, [ NULL | ‘value’ ]<br>-返回全部的first_name名，并且把全部的T都替换为B</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> first_name, replace(first_name, <span class="hljs-string">&#x27;T&#x27;</span>, <span class="hljs-string">&#x27;B&#x27;</span>)<br><span class="hljs-keyword">FROM</span> sales.employee_tbl;<br></code></pre></td></tr></table></figure>
<p>返回员工表里的全部城市，并且把城市名称里的I都替换为Z</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> city, replace(city, <span class="hljs-string">&#x27;l&#x27;</span>, <span class="hljs-string">&#x27;Z&#x27;</span>)<br><span class="hljs-keyword">FROM</span> sales.employee_tbl;<br></code></pre></td></tr></table></figure>

<h3 id="UPPER-x2F-LOWER字符函数"><a href="#UPPER-x2F-LOWER字符函数" class="headerlink" title="UPPER&#x2F;LOWER字符函数"></a>UPPER&#x2F;LOWER字符函数</h3><p>大多数实现都提供了控制数据大小写的函数。UPPER函数可以把字符串里的小写字母转化为大写,与UPPER函数相反，LOWER把字符串里的大写字符转化为小写。<br>下面的语句把字段里产品描述所有的字符都转化为大写和小写<br>SELECT prod_desc, UPPER(prod_desc), LOWER(prod_desc) FROM sales.products_tbl;</p>
<h3 id="INSTR字符函数"><a href="#INSTR字符函数" class="headerlink" title="INSTR字符函数"></a>INSTR字符函数</h3><p>INSTR函数用于在字符串里寻找指定的字符串，返回其所在的字符第一个字符开始的位置。ANSI语法如下所示:<br>INSTR(column_name, search_string [,start_position [, occurence ]]<br>其中column_name表示被查找的字符串; search_ string表示要查找的字符串; start position参数可选，表示开始寻找的位置;occurrence表示指定第几次出现，如果无法找到返回结果0<br>MySQL的语法有所不同，没有后面两个参数，只能查找首次出现的位置</p>
<p>例子</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">select</span> city,instr(city,<span class="hljs-string">&#x27;I&#x27;</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>) <span class="hljs-keyword">from</span> employee_tbl;<br></code></pre></td></tr></table></figure>
<p>. Oracle数据库返回城市字段从的第3个字符开始，返回I字符第2次出现的位置</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">select</span> emp_id,instr(emp_id,<span class="hljs-string">&#x27;5&#x27;</span>) <span class="hljs-keyword">from</span> sales.employee_tbl;<br></code></pre></td></tr></table></figure>
<p>. MySQL返回emp_id的字符5出现的位置</p>
<h3 id="TRIM-x2F-LTRIM-x2F-RTRIM字符函数"><a href="#TRIM-x2F-LTRIM-x2F-RTRIM字符函数" class="headerlink" title="TRIM&#x2F;LTRIM&#x2F;RTRIM字符函数"></a>TRIM&#x2F;LTRIM&#x2F;RTRIM字符函数</h3><p>TRIM、LTRIM和RTRIM函数是另一种截取部分字符串的方式，它与SUBSTRING属于同一家族。分别用于从左右两边、左边、右边删除字符串里的字符，其语法如下所示:<br>. TRIM (characster_string, [‘set’])<br>. LTRIM(characster_string, [‘set’])<br>. RTRIM(characster_string, [‘set’])<br>对于MySQL来说，无法指定删除的字符串，只能将两边或左边或右边的空格删除</p>
<p>例子：<br>oracle函数将city字段左边IN字符删除<br>select city,LTRIM(city,’IN’) from employee_tbl;</p>
<h3 id="LENGTH函数"><a href="#LENGTH函数" class="headerlink" title="LENGTH函数"></a>LENGTH函数</h3><p>LENGTH函数是很常见的，用于得到字符串、数字、日期或表达式的长度(单位是字节,)其语法如下所示:<br>LENGTH(character_string)</p>
<p>例子<br>Oracle函数将city字段左边IN字符删除</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> position,<br>date_hire,<br>LENGTH(position),LENGTH(date_hire)<br><span class="hljs-keyword">FROM</span> sales.employee_pay_tbl;<br></code></pre></td></tr></table></figure>

<h3 id="IFNULL函数"><a href="#IFNULL函数" class="headerlink" title="IFNULL函数"></a>IFNULL函数</h3><p>IFNULL函数用于在一个表达式是NULL时从另一个表达式获得值。它可以用于大多数数据类型，但值与替代值必须是同一数据类型。其语法如下所示:<br>IFNULL(value1, value2)<br>例子<br>·查询NULL值，并且用123456789代替NULL值:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> pager,<br>IFNULL(pager, <span class="hljs-string">&#x27;123456789&#x27;</span>) <span class="hljs-keyword">FROM</span> sales.employee_tbl;<br></code></pre></td></tr></table></figure>
<p>·应用在两个字段的算数操作，例如将salary和bonus值下相加，由于<br>这两字段允许为NULL，所以需要是IFNUUL进行装换操作，保障结果的正确性</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> salary,<br>bonus,<br>salary <span class="hljs-operator">+</span> bonus,<br>IFNULL(salary,O)<span class="hljs-operator">+</span> IFNULL(bonus, <span class="hljs-number">0</span>) <span class="hljs-keyword">FROM</span> sales.employee_pay_tbl;<br></code></pre></td></tr></table></figure>

<h3 id="COALESCE函数"><a href="#COALESCE函数" class="headerlink" title="COALESCE函数"></a>COALESCE函数</h3><p>COALESCE函数也是用指定值替代NULL值，这一点与IFNULL是一样的。其不同点在于，它可以接受一个数据集，依次检查其中每一个值，直到发现一个非NULL值。如果没有找到非NULL值，它会返回一个NULL值。<br>例子<br>. COALESCE函数返回BONUS、SALARY和PAY_RATE字段里第一个非NULL值。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> salary,<br>bonus,pay_rate,<br><span class="hljs-built_in">COALESCE</span>(salary, bonus, pay_rate)<span class="hljs-keyword">FROM</span> sales.employee_pay_tbl;<br></code></pre></td></tr></table></figure>

<h3 id="LPAD-x2F-RPAD函数"><a href="#LPAD-x2F-RPAD函数" class="headerlink" title="LPAD&#x2F;RPAD函数"></a>LPAD&#x2F;RPAD函数</h3><p>LPAD(左填充)&#x2F;RPAD(有填充)用于在字符串左侧&#x2F;右侧添加指定的字符，其语法如下所示:<br>. LPAD(str, len, padstr)<br>. RPAD(str, len, padstr)<br>例子<br>·范例在每个产品描述左侧&#x2F;右侧添加句点，使其总长度达到20个字符</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">select</span> prod_desc,length(prod_desc) <span class="hljs-keyword">as</span> len,<br>lpad(prod_desc,<span class="hljs-number">20</span>,<span class="hljs-string">&#x27;.&#x27;</span>),rpad(prod_desc,<span class="hljs-number">20</span>,<span class="hljs-string">&#x27;.&#x27;</span>) <span class="hljs-keyword">from</span> prod_tbl;<br></code></pre></td></tr></table></figure>


<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> salary,<br>bonus,pay_rate,<br><span class="hljs-built_in">COALESCE</span>(salary, bonus, pay_rate)<span class="hljs-keyword">FROM</span> sales.employee_pay_tbl;<br></code></pre></td></tr></table></figure>
<p>注意如果原始值超过指定的长度结果值会被截短</p>
<h3 id="ASCII函数"><a href="#ASCII函数" class="headerlink" title="ASCII函数"></a>ASCII函数</h3><p>ASCII函数返回字符串参数第一个字符的ASCII(美国信息交换标准码)，其语法如下所示:ASCll(character_string)<br>下面是一些例子<br>ASCII(‘A’)返回65<br>ASCII(‘B’)返回66<br>ASCII(‘C’)返回67<br>ASCII(‘a’)返回97</p>
<h3 id="算数函数"><a href="#算数函数" class="headerlink" title="算数函数"></a>算数函数</h3><p>算术函数可以对数据库里的值根据算术规则进行运算，在不同类型数据库中，算术函数相对很标准:</p>
<ul>
<li>绝对值– ABS</li>
<li>四舍五入-ROUND(X), ROUND(X,D)<br>没有指定参数D时表示取证，当指定参数且为正数表示保留小数D，为负数时表示保留整数位的个数</li>
<li>平方根-SQRT</li>
<li>符号-SIGN(X)<br>返回参数的符号值，使用1,0,-1分别表示参数是否整数、0或者负数</li>
<li>幂运算一 POWER</li>
<li>上限和下限-CEIL(X)&#x2F;FLOOR(X)<br>CEIL返回大于等于参数的最小整数值，FLOOR返回小于等于参数的最大整数值</li>
<li>指数-EXP</li>
<li>三角函数- SIN&#x2F;COS&#x2F;TAN</li>
</ul>
<h3 id="CAST-x2F-CONVERT转换函数"><a href="#CAST-x2F-CONVERT转换函数" class="headerlink" title="CAST&#x2F;CONVERT转换函数"></a>CAST&#x2F;CONVERT转换函数</h3><p>转换函数把数据类型从一种转换为另一种。举例来说，我们的数据通常是以字符形式保存的，但为了计算就需要把它转换为数值。算术函数和计算不能用于以字符形式表示的数据。下面是一些常见的数据转换:</p>
<ul>
<li>字符到数字</li>
<li>数字到字符</li>
<li>字符到日期</li>
<li>日期到字符<br>转换函数的语法为:<br>CAST(expr As type [ARRAY])<br>CONVERT(expr, type)<br>type可以是BINARY、CHAR、DATE、DATETIME、DECIMAL、DOUBLE、FLOAT、JSON、SIGNED、UNSIGNED等MySQL定义的数据类型</li>
</ul>
<h3 id="函数的组合使用"><a href="#函数的组合使用" class="headerlink" title="函数的组合使用"></a>函数的组合使用</h3><p>大多数函数可以在SQL语句里组合使用，多个函数组合使用，SQL就能够有很大的灵活性，完成各种各种的数据处理。<code>当SQL语句的函数内部嵌有函数时，最内层的函数首先被处理，然后从里向外依次执行各个函数</code><br>例子<br>·在一个查询里组合使用两个函数(串接和子串)，把 EMP_ID字段分为3<br>部分，再用短划线把它们连接起来，从而得到更清晰易读的社会保险号码。范例里使用了CONCAT函数来组合字符串。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> CONCAT(last_name, <span class="hljs-string">&#x27;. &#x27;</span>, first_name) <span class="hljs-keyword">AS</span> name,<br>CONCAT(SUBSTR(emp_id,<span class="hljs-number">1</span>,<span class="hljs-number">3</span>), <span class="hljs-string">&#x27;-&#x27;</span>,<br>    SUBSTR(emp_id, <span class="hljs-number">4</span>,<span class="hljs-number">2</span>), <span class="hljs-string">&#x27;-&#x27;</span>,<br>    SUBSTR(emp_id, <span class="hljs-number">6</span>,<span class="hljs-number">4</span>))<span class="hljs-keyword">AS</span> id <span class="hljs-keyword">FROM</span> sales.employee_tbl;<br></code></pre></td></tr></table></figure>
<p>·使用LENGTH函数和算术运算符(+)把每个字段的姓和名的长度加在一起，然后SUM函数返回所有姓和名的长度之和。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">SUM</span>(LENGTH(last_name)<span class="hljs-operator">+</span> LENGTH(first_name))<span class="hljs-keyword">As</span> total_lengthFROM sales.employee_tbl;<br></code></pre></td></tr></table></figure>



<p>例：编写一个SQL语句，以以下形式列出员工的姓名、ID和电话号码（表：employee_tbl）</p>
<p>a) 姓名显示为Smith， John</p>
<p>b) 员工ID显示为999-99-9999格式</p>
<p>c) 电话号码显示为（999）999-9999</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">select</span> concat(first_name, <span class="hljs-string">&#x27; &#x27;</span>, last_name)                                                   <span class="hljs-keyword">as</span> <span class="hljs-string">&#x27;员工姓名&#x27;</span>,<br>       concat(substr(emp_id, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>), <span class="hljs-string">&#x27;-&#x27;</span>, substr(emp_id, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>), <span class="hljs-operator">+</span><span class="hljs-string">&#x27;-&#x27;</span>, substr(emp_id, <span class="hljs-number">6</span>, <span class="hljs-number">9</span>))  <span class="hljs-keyword">as</span> <span class="hljs-string">&#x27;员工ID&#x27;</span>,<br>       concat(<span class="hljs-string">&#x27;(&#x27;</span>, substr(phone, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>), <span class="hljs-string">&#x27;)&#x27;</span>, substr(phone, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>), <span class="hljs-string">&#x27;-&#x27;</span>, substr(phone, <span class="hljs-number">7</span>, <span class="hljs-number">4</span>)) <span class="hljs-keyword">as</span> <span class="hljs-string">&#x27;员工电话&#x27;</span><br><span class="hljs-keyword">from</span> employee_tbl;<br></code></pre></td></tr></table></figure>


                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%A4%A7%E6%95%B0%E6%8D%AE/" class="category-chain-item">大数据</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%A4%A7%E6%95%B0%E6%8D%AE/">#大数据</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>DB12_数据库函数1</div>
      <div>http://example.com/2022/12/26/数据库/MySQL/大数据学习/DB12&amp;DB13/DB12_数据库函数1/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>深海火锅店</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年12月26日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/12/27/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB14&amp;DB15/DB14_%E8%A1%A8%E8%BF%9E%E6%8E%A5/" title="DB14_表连接">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">DB14_表连接</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/12/26/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB12&amp;DB13/DB13_%E6%95%B0%E6%8D%AE%E5%BA%93%E5%87%BD%E6%95%B02/" title="DB13_数据库函数1">
                        <span class="hidden-mobile">DB13_数据库函数1</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/DynamicRibbon.min.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
